//
//  HMHomeTableViewController.swift
//  WeiBoXR
//
//  Created by acer on 16/9/19.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit

import YYModel
private let cellID = "cellID"
class HMHomeTableViewController: HMVisitorTableViewController {

    lazy var statusListViewModel = HMStatusListViewModel()
    
    fileprivate lazy var pullUPView : UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.red
        return view
    }()
//    fileprivate lazy var pullDownView : UIRefreshControl = {
//        let view = UIRefreshControl()
//        //  添加下拉刷新事件
//        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
//        return view
//    }()
    fileprivate lazy var pullDownCtr: HMRefreshing = {
       let view = HMRefreshing()
        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        return view
    }()
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.white, fontSize: 12)
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.text = "没有加载到最新的微博数据"
        label.isHidden = true
        return label
        
    }()

    override func viewDidLoad() {
        super.viewDidLoad()


        if !isLogin {
            visitorView?.updataVisitorInfo(message: nil, imageName: nil)
        } else {
            setupUI()
            setupTableview()
            loadData()
        }
    }
    private func setupUI() {
        
        if let nav = self.navigationController {
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
    }
    //  开启tip动画
    private func startAnimation(message: String) {
        
        //  当前tipLabel如果显示表示正在执行动画,那么不需要再次执行
        if tipLabel.isHidden == false {
            return
        }
        tipLabel.text = message
        //  显示label
        tipLabel.isHidden = false
        UIView.animate(withDuration: 1,animations: {
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
            
        }) { (_) in
                UIView.animate(withDuration: 1, animations: { 
                    self.tipLabel.transform = CGAffineTransform.identity
                    }, completion: { (_) in
                        self.tipLabel.isHidden = true
                })
        }
    }
    private func endRefreshing() {
        //  结束刷新
        pullUPView.stopAnimating()
//        pullDownView.endRefreshing()
        pullDownCtr.endRefreshing()
    }
    @objc private func pullDownRefreshing() {
        loadData()
    }
    fileprivate func loadData() {
        
        statusListViewModel.loadData(isPullUP: pullUPView.isAnimating) { (issuccess, message) in
            
            if !self.pullUPView.isAnimating {
                self.startAnimation(message: message)
            }
            self.endRefreshing()
            if issuccess {
                self.tableView.reloadData()
            }
        }
    }
    private func setupTableview() {
        tableView.register(HMHomeTableViewCell.self, forCellReuseIdentifier: cellID)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .none
        tableView.tableFooterView = pullUPView
//        self.refreshControl = pullDownView
        tableView.addSubview(pullDownCtr)
    }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
}


extension HMHomeTableViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:HMHomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! HMHomeTableViewCell
        let viewModel: HMHomeCellViewModel = statusListViewModel.statusList[indexPath.row]
        cell.statusViewModel = viewModel
        cell.selectionStyle = .none
//        cell.backgroundColor = RandomColor()
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullUPView.isAnimating {
            self.pullUPView.startAnimating()
            loadData()
        }
    }
}
